package com.tanhua.autoconfig.template;

import com.aliyun.imageaudit20191230.Client;
import com.aliyun.imageaudit20191230.models.*;
import com.aliyun.imageaudit20191230.models.ScanImageRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.imageaudit20191230.models.ScanTextRequest;
import com.aliyun.imageaudit20191230.models.ScanTextResponse;
import com.aliyun.teaopenapi.models.Config;
import com.tanhua.autoconfig.properties.GreenProperties;
import lombok.extern.slf4j.Slf4j;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.imageaudit.model.v20191230.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: itheima
 * @create: 2021-05-31 00:46
 */
@Slf4j
public class AliyunGreenTemplate {

    private Client client;

    private GreenProperties greenProperties;

    public AliyunGreenTemplate(GreenProperties greenProperties) {
        this.greenProperties = greenProperties;
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(greenProperties.getAccessKeyID())
                // 您的AccessKey Secret
                .setAccessKeySecret(greenProperties.getAccessKeySecret());
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        try {
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 阿里云文本内容检查
     *
     * @param content
     * @return map  key - suggestion内容
     * pass：文本正常，可以直接放行，
     * review：文本需要进一步人工审核，
     * block：文本违规，可以直接删除或者限制公开
     * value -   通过，或 出错原因
     * @throws Exception
     */
    public Map<String, String> greenTextScan(String content) throws Exception {

        String scenes = greenProperties.getScenes();
        String[] scansArr = scenes.split(",");
        List<ScanTextRequest.ScanTextRequestLabels> list = new ArrayList<>();
        if(scansArr!=null && scansArr.length>0){
            for (String s : scansArr) {
                ScanTextRequest.ScanTextRequestLabels labels = new ScanTextRequest.ScanTextRequestLabels()
                        .setLabel(s);
                list.add(labels);
            }
        }

        ScanTextRequest.ScanTextRequestTasks tasks0 = new ScanTextRequest.ScanTextRequestTasks()
                .setContent(content);
        ScanTextRequest scanTextRequest = new ScanTextRequest()
                .setTasks(java.util.Arrays.asList(
                        tasks0
                ))
                .setLabels(list);
        // 复制代码运行请自行打印 API 的返回值
        ScanTextResponse scanTextResponse = client.scanText(scanTextRequest);
        ScanTextResponseBody body = scanTextResponse.getBody();
        System.out.println(body);
        String suggestion = body.data.elements.get(0).results.get(0).suggestion;
        String label = body.data.elements.get(0).results.get(0).label;
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("suggestion",suggestion);
        if (suggestion.equals("review")) {
            resultMap.put("reson", "文章内容中有不确定词汇");
            log.info("返回结果，resultMap={}", resultMap);
            return resultMap;
        } else if (suggestion.equals("block")) {
            String reson = "文章内容中有敏感词汇";
            if (label.equals("spam")) {
                reson = "文章内容中含垃圾信息";
            } else if (label.equals("ad")) {
                reson = "文章内容中含有广告";
            } else if (label.equals("politics")) {
                reson = "文章内容中含有涉政";
            } else if (label.equals("terrorism")) {
                reson = "文章内容中含有暴恐";
            } else if (label.equals("abuse")) {
                reson = "文章内容中含有辱骂";
            } else if (label.equals("porn")) {
                reson = "文章内容中含有色情";
            } else if (label.equals("flood")) {
                reson = "文章内容灌水";
            } else if (label.equals("contraband")) {
                reson = "文章内容违禁";
            } else if (label.equals("meaningless")) {
                reson = "文章内容无意义";
            }
            resultMap.put("reson", reson);
            log.info("返回结果，resultMap={}", resultMap);
            return resultMap;
        }
        return resultMap;
    }

    /**
     * 阿里云图片内容安全
     */
    public Map<String,String> imageScan(List<String> imageList) throws Exception {
        Map<String, String> resultMap = new HashMap<>();
        String scenes = greenProperties.getScenes();
        String[] scansArr = scenes.split(",");
        ScanImageRequest request = new ScanImageRequest();
        List<ScanImageRequest.ScanImageRequestTask> taskList = new ArrayList<ScanImageRequest.ScanImageRequestTask>();
        ScanImageRequest.ScanImageRequestTask task1 = new ScanImageRequest.ScanImageRequestTask();
        for (String s : imageList) {
            task1.setDataId(UUID.randomUUID().toString());
            task1.setImageURL(s);
            taskList.add(task1);
        }
        request.setTask(taskList);
        List<String> sceneList = new ArrayList<String>();
        sceneList.add("ad");
        request.setScene(sceneList);
        ScanImageResponse response = client.scanImage(request);
        ScanImageResponseBody body = response.getBody();
        String suggestion = body.data.results.get(0).subResults.get(0).suggestion;
        String label = body.data.results.get(0).subResults.get(0).label;
        if (!suggestion.equals("pass")) {
            resultMap.put("suggestion", suggestion);
            resultMap.put("label", label);
            return resultMap;
        }
        resultMap.put("suggestion", "pass");
        return resultMap;
    }
}